<template>
	<view class="container">

		<!-- 表单内容 -->
		<view class="form-content">
			<!-- 自我评价 -->
			<view class="form-section">
				<view class="label">自我评价:</view>
				<textarea v-model="form.selfEvaluation" placeholder="个人自我介绍.." class="textarea-input" :maxlength="500"
					:auto-height="true" :show-confirm-bar="false" />
				<view class="word-count">{{ form.selfEvaluation.length }}/500</view>
			</view>

			<!-- 任教经验 -->
			<view class="form-section">
				<view class="label">任教经验:</view>
				<textarea v-model="form.teachingExperience" placeholder="任教相关经验.." class="textarea-input"
					:maxlength="500" :auto-height="true" :show-confirm-bar="false" />
				<view class="word-count">{{ form.teachingExperience.length }}/500</view>
			</view>

			<!-- 证书描述 -->
			<view class="form-section">
				<view class="label">证书描述:</view>
				<textarea v-model="form.certificateDescription" placeholder="所获各类证书.." class="textarea-input"
					:maxlength="500" :auto-height="true" :show-confirm-bar="false" />
				<view class="word-count">{{ form.certificateDescription.length }}/500</view>
			</view>
		</view>

		<!-- 提交按钮 -->
		<view class="submit-button">
			<u-button type="warning" @click="nextStep">下一步</u-button>
		</view>
	</view>
</template>

<script>
	import {
		submitTeacherIntro
	} from '@/utils/mockApi.js';
	import * as api from '@/api/family'
	export default {
		data() {
			return {
				orderId: '', // 从上一页传递过来的订单ID
				form: {
					selfEvaluation: '',
					teachingExperience: '',
					certificateDescription: '',
					id: 0
				}
			};
		},
		onLoad(options) {
			// 接收上一页传递的订单ID
			if (options.orderId) {
				this.orderId = options.orderId;
				this.form.id = options.orderId
				console.log('接收到订单ID:', this.orderId);
			}
		},
		methods: {
			goBack() {
				uni.navigateBack();
			},

			/**
			 * 验证表单数据
			 * @returns {boolean}
			 */
			validateForm() {
				if (!this.form.selfEvaluation.trim()) {
					uni.showToast({
						title: '请填写自我评价',
						icon: 'none'
					});
					return false;
				}

				if (this.form.selfEvaluation.trim().length < 20) {
					uni.showToast({
						title: '自我评价至少20字',
						icon: 'none'
					});
					return false;
				}

				if (!this.form.teachingExperience.trim()) {
					uni.showToast({
						title: '请填写任教经验',
						icon: 'none'
					});
					return false;
				}

				if (this.form.teachingExperience.trim().length < 20) {
					uni.showToast({
						title: '任教经验至少20字',
						icon: 'none'
					});
					return false;
				}

				if (!this.form.certificateDescription.trim()) {
					uni.showToast({
						title: '请填写证书描述',
						icon: 'none'
					});
					return false;
				}

				if (this.form.certificateDescription.trim().length < 10) {
					uni.showToast({
						title: '证书描述至少10字',
						icon: 'none'
					});
					return false;
				}

				return true;
			},

			/**
			 * 提交自我介绍数据
			 */
			async nextStep() {
				// 验证表单
				if (!this.validateForm()) {
					return;
				}

				// 显示加载提示
				uni.showLoading({
					title: '提交中...',
					mask: true
				});

				try {
					// 准备提交的数据
					const submitData = {
						selfEvaluation: this.form.selfEvaluation.trim(),
						teachingExperience: this.form.teachingExperience.trim(),
						certificateDescription: this.form.certificateDescription.trim(),
						id:this.form.id.trim(),
					};

					// 调用mock API提交数据
					await api.insertTeacherApi(submitData).then(response=>{
						uni.hideLoading();
						
						if (response.code === 200) {
							// 提交成功
							uni.showToast({
								title: response.message || '提交成功',
								icon: 'success',
								duration: 2000
							});
						
							// 延迟跳转到认证页面
							setTimeout(() => {
								uni.navigateTo({
									url: `/pages/jiajiao/teacher_certification?orderId=${this.orderId || response.data.orderId}`
								});
							}, 2000);
						} else {
							// 提交失败
							uni.showModal({
								title: '提交失败',
								content: response.message || '提交失败，请重试',
								showCancel: false
							});
						}
					});

					
				} catch (error) {
					uni.hideLoading();
					console.error('提交自我介绍失败:', error);
					uni.showModal({
						title: '提交失败',
						content: '网络错误，请检查网络后重试',
						showCancel: false
					});
				}
			}
		}
	};
</script>

<style lang="scss" scoped>
	@import "@/uni.scss";

	.container {
		padding: 24rpx;
		padding-bottom: 120rpx;
		/* 为底部按钮留出空间 */
		background-color: #f8f8f8;
		min-height: 100vh;
	}

	.header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		background-color: #00c8c8;
		padding: 10rpx 20rpx;
		color: #fff;
		margin: -24rpx -24rpx 20rpx -24rpx;
	}

	.header-title {
		font-size: 36rpx;
		font-weight: bold;
	}

	.form-section {
		background-color: #ffffff;
		border-radius: 8rpx;
		padding: 30rpx;
		margin-bottom: 20rpx;
		box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1);
	}

	.label {
		font-weight: bold;
		margin-bottom: 20rpx;
		font-size: 28rpx;
		color: #333;
	}

	.submit-button {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		background-color: white;
		padding: 20rpx;
		border-top: 2rpx solid #f0f0f0;
		z-index: 100;
	}

	/* 原生textarea样式 */
	.textarea-input {
		width: 100%;
		min-height: 200rpx;
		padding: 20rpx;
		border: 2rpx solid #e0e0e0;
		border-radius: 8rpx;
		background-color: #fafafa;
		font-size: 28rpx;
		line-height: 1.6;
		color: #333;
		box-sizing: border-box;
		resize: none;
		word-wrap: break-word;
		word-break: break-all;
		transition: all 0.3s ease;
	}

	.textarea-input:focus {
		border-color: #ff6b35;
		background-color: #fff;
		outline: none;
	}

	.textarea-input::placeholder {
		font-size: 28rpx;
		color: #999;
		line-height: 1.6;
	}

	/* 字数统计样式 */
	.word-count {
		font-size: 24rpx;
		color: #999;
		text-align: right;
		margin-top: 10rpx;
		padding-right: 10rpx;
	}

	/* 按钮样式 */
	::v-deep .u-button--warning {
		background: linear-gradient(135deg, #ff9500 0%, #ff6b00 100%);
		border: none;
		border-radius: 8rpx;
		height: 80rpx;
		font-size: 28rpx;
		font-weight: bold;
	}
</style>